<?php
/**
 * Created by PhpStorm.
 * User: dream
 * Date: 2017/8/20
 * Time: 16:15
 */
class Mood extends MY_Controller
{

    //发布说说(心情)

    public function publish()
    {

        $this->loadHelper('url');
        if($this->isGet())
        {

            $this->loadModel("userinfo");
            $this->loadModel("usermood");

            $base_url = web_url('mood/publish',true);
            $offset = $this->uri->segment(4);
            $per_page = 3;
            $sess_user = $this->session("user");
            $db_users = $this->userinfo->getWhere(["id"=>$sess_user->id]);
            $db_moods = $this->usermood->getWhere(["user_id"=>$sess_user->id],$offset,$per_page,false);
            $count = $this->usermood->getRows(["user_id"=>$sess_user->id]);
            $db_user = reset($db_users); //信息表

            $data_arr = ['db_user'=>$db_user,'sess_user'=>$sess_user,'db_moods'=>$db_moods];

            $this->_mood_list($base_url,$count,$per_page,$data_arr);
        }
        else{

            $message = $this->post("message");
            $this->loadModel("usermood");
            $user_id =$this->session("user")->id;
            $data=[
                'user_id'=>$user_id,
                'content'=>$message,
                'ctime'=>$this->time()
            ];
           $res =  $this->usermood->add($data);
           if($res)
           {
               echo json_encode(['code'=>1,'message'=>$message,'data'=>$data]);
           }else{
               echo json_encode(['code'=>0,'message'=>'发布失败!']);
           }

        }

    }

    private function _mood_list($base_url,$count,$per_page,$view_data)
    {
        $this->loadHelper("url");
        $links_str = $this->bootstrapPagination($base_url,$count,$per_page,true);
        $view_data['links']=$links_str;
        $this->loadView(ADMIN.'mood/mood_publish',$view_data);
    }

    //已经发布的说说列表  分页

    public function index()
    {

        $this->loadView(ADMIN.'mood/mood_index');
    }

    //草稿箱

    public function draft()
    {

        $this->loadView(ADMIN.'mood/mood_draft');
    }

    //评论列表

    public function comments()
    {


        $this->loadView(ADMIN.'mood/mood_comments');
    }



}